package ru.hh.team02.chat.messaging;

import java.util.List;
import java.util.Set;

import ru.hh.team02.chat.client.Client;
import ru.hh.team02.chat.database.DatabaseServiceInterface;
import ru.hh.team02.chat.server.Services;

/**
 * class responsible for macro sending logic. current version - one queue with
 * messages per active client
 */
public class SendingService {

    /**
     * send personal message
     * @param message - message text
     * @param recipientName
     */
    public void sendPersonalMessage(String message, String recipientName, String senderName, DatabaseServiceInterface dbService) {
        if (!dbService.clientExists(recipientName)) {
            Client recipient = Services.registry.getClientByName(senderName);
            recipient.add(new NormalMessage("server> you are trying to communicate with unknown client"));

            Services.ADMIN_LOGGER.write("Client \"" + senderName
                    + "\" tried to communicate with unknown client");

            return;
        }

        // put message to database
        int id = dbService.putNewPersonalMessageIntoDatabase(message, recipientName);

        // try send it to use
        if (Services.registry.hasClientWithName(recipientName)) {
            Client recipient = Services.registry.getClientByName(recipientName);
            recipient.add(new PrivateMessage(message, id));

            Services.ADMIN_LOGGER.write("Client \"" + senderName
                    + "\" sent new message: \""
                    + message + "\" to \"" + recipientName + "\"");

        }

    }


    /**
     * Send all undelivered messages to the recipient which will be taken from the DB.
     * @param recipientName Recipient name
     */
    public void sendUndeliveredMessages(String recipientName, DatabaseServiceInterface dbService) {
        List<Message> messages = dbService.getAllUndeliveredMessagesToTheClient(recipientName);

        if (Services.registry.hasClientWithName(recipientName)) {
            Client recipient = Services.registry.getClientByName(recipientName);
            recipient.addAllFront(messages);
        }
    }


    /**
     * Send server message
     * @param message Message text
     * @param recipientName Recipient name
     */
    public void sendServiceMessage(String message, String recipientName) {
        if (Services.registry.hasClientWithName(recipientName)) {
            Client recipient = Services.registry.getClientByName(recipientName);
            recipient.add(new NormalMessage(message));
        }
    }

    /**
     * Put message to all personal queues of people, who are in the same room as
     * sender
     *
     * @param text
     *            Message text
     * @param senderName
     *            Sender name
     */
    // XXX [Oleg] Why do we need here senderName, and the other methods don't have this parameter.
    // XXX [Oleg] This inconsistency is very noticeable and unpleasant. It is justified?
    public void sendMessageToRoom(String text, String senderName) {
        String room = Services.registry.getClientRoom(senderName);
        Set<Client> recipients = Services.registry.getClientsFromRoom(room);

        for (Client recipient : recipients) {
            recipient.add(new NormalMessage(text));
        }

    }
}
